/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db;

import java.sql.ResultSet;
import java.util.ArrayList;
import models.Supplier;

/**
 *
 * @author Dan
 */
public class SupplierDb extends AbstractDb implements SupplierInterface {

    public boolean insertSup(Supplier sup) {
        boolean succes = false;
        try { // insert new supplier
            int rc = this.db.queryInsert("INSERT INTO supplier (supplier_email, supplier_name, supplier_address, supplier_country, supplier_phoneNo)  VALUES("
                    + "'" + sup.getemail() + "',"
                    + "'" + sup.getName() + "',"
                    + "'" + sup.getAddress() + "',"
                    + "'" + sup.getCountry() + "',"
                    + "'" + sup.getPhoneno() + "');");
            if (rc > 0) {
                succes = true;

                // Her Dan. Hvis du vil have det ID som du lige har oprettet
                int lastId = this.db.getInsertId();
            }

        }//end try
        catch (Exception ex) {
            System.out.println("Supplier was not created");
            System.out.println("Insert exception in supplier db: " + ex);

        }
        return (succes);

    }

    public boolean updateSup(Supplier sup) {
        Supplier supObj = sup;
        boolean succes = false;
        try { // Update supplier
            int rc = this.db.queryUpdate("UPDATE supplier SET "
                    + "supplier_email='" + supObj.getemail() + "', "
                    + "supplier_name='" + supObj.getName() + "', "
                    + "supplier_address='" + supObj.getAddress() + "', "
                    + "supplier_country='" + supObj.getCountry() + "', "
                    + "supplier_phoneNo='" + supObj.getPhoneno() + "' "
                    + "WHERE supplier_id='" + supObj.getId() + "';");
            if (rc > 0) {
                succes = true;
            }
        }//end try
        catch (Exception ex) {
            System.out.println("Supplier was not Update");
            System.out.println("Update exception in supplier db: " + ex);
        }
        return succes;

    }

    public ArrayList<Supplier> getAllSupplier() {
        ArrayList<Supplier> supList = new ArrayList<Supplier>();

        return supList;
    }

    public ArrayList<Supplier> searchSupplier(String name) {
        ArrayList<Supplier> supList = new ArrayList<Supplier>();

        if (name.length() > 0) {
            try {
                String query = "";
                query = "supplier_name LIKE '%" + name + "%'";

                ResultSet items = this.db.query("SELECT * FROM Supplier WHERE " + query);
                if (items != null) {
                    while (items.next()) {
                        Supplier supplier = new Supplier(
                                items.getInt("supplier_id"),
                                items.getString("supplier_email"),
                                items.getString("supplier_name"),
                                items.getString("supplier_address"),
                                items.getString("supplier_country"),
                                items.getLong("supplier_phoneNo"));
                        supList.add(supplier);
                    }
                }
            } catch (Exception e) {
                System.out.println("there was an error while searching for Suppliers");
            }
        }
        return supList;
    }

    public Supplier getSupplier(int id) {
        Supplier supObj = null;
        if (id > 0) {
            try {
                String query = "";
                query = "supplier_id=" + id;

                ResultSet rs = this.db.query("SELECT * FROM Supplier WHERE " + query);
                if (rs != null && rs.next()) {
                    int uid = rs.getInt("supplier_id");
                    //System.out.println("uid: " + uid);
                    String email = rs.getString("supplier_email");
                    String name = rs.getString("supplier_name");
                    String address = rs.getString("supplier_address");
                    String country = rs.getString("supplier_country");
                    long phoneno = rs.getLong("supplier_phoneNo");
                    supObj = new Supplier(uid, email, name, address, country, phoneno);
                }

            } catch (Exception e) {
            }
        }
        return supObj;
    }

    public int deleteSupplier(int id) {
        int rc = -1;
        try { // delete from supplier
            String query = "DELETE FROM supplier "
                    + "WHERE supplier_id = '" + id + "'";

            rc = this.db.queryUpdate(query);
        }//end try
        catch (Exception ex) {
            System.out.println("supplier db Delete exception: " + ex);
        }
        return (rc);
    }
}
